package com.orientation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orientation.commom.exception.CustomException;
import com.orientation.entity.Class;
import com.orientation.entity.Major;
import com.orientation.mapper.ClassMapper;
import com.orientation.service.ClassService;
import com.orientation.service.MajorService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * <p>
 * 班级 服务实现类
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {
    @Resource
    private ClassMapper classMapper;
    @Resource
    private MajorService majorService;


    @Override
    public List<Class> queryClassesList(String collegeId, String majorId) {
        LambdaQueryWrapper<Class> queryClass = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(collegeId)) {
            queryClass.eq(Class::getCollegeId, collegeId);
        }
        if (StringUtils.hasLength(majorId)) {
            queryClass.eq(Class::getMajorId, majorId);
        }
        List<Class> classList = classMapper.selectList(queryClass);
        List<Class> classArrayList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(classList)) {
            //去重
            List<Class> classes = classList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(c -> c.getClassName()))), ArrayList::new));
            classArrayList = classes.stream().sorted(Comparator.comparing(Class::getClassName)).collect(Collectors.toList());
        }

        return classArrayList;
    }

    @Override
    public void insertClass(Class classes) {
        if (classes != null && classes.getClassName() != null) {
            Class aClass = classMapper.selectOne(new LambdaQueryWrapper<Class>().eq(Class::getClassName, classes.getClassName()));
            if (aClass != null) {
                throw new CustomException("18", "此班级已经存在，请不要重复添加");
            }
            // if (!StringUtils.hasLength(classes.getClassCode())) {
            //     throw new CustomException("18", "请填写班级编码");
            // }
            // if (StringUtils.hasLength(classes.getClassCode()) && classes.getClassCode().length() != 2) {
            //     throw new CustomException("18", "班级编码错误(应为两位数)");
            // }
            if (!StringUtils.hasLength(classes.getMajorId())) {
                throw new CustomException("18", "专业信息必填");
            }
            List<Class> classList = classMapper.selectList(new LambdaQueryWrapper<Class>().eq(Class::getMajorId, classes.getMajorId()));
            Major major = majorService.getById(classes.getMajorId());
            classes.setAdviserId(major.getAdviserId());
            classes.setAdviser(major.getAdviser());
            classes.setClassCode(classList.size() + 1);
            this.save(classes);
        }
    }
}
